﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using SERP.StringMappings;
using SERP.DAO;
using SERP.Controllers;
namespace SERP.Modules.SalaryFields{
	public partial class SalaryFieldsList : SERP.Modules.ModuleBase
	{
		private SalaryFieldsController controller = new SalaryFieldsController();    
		protected void Page_Load(object sender, EventArgs e){
			LoadLanguage();
			if (!this.IsPostBack)
			{			
				LoadData();
			}     
		}
		private int GetPageIndex(){
			int pageIndex = 0;
			if (Request["PageIndex"] != null)
			{
				try{
					pageIndex = int.Parse(Request["PageIndex"]);
				}
				catch{
				}
			}
			return pageIndex;
		}
		private string GetFilter(){
			string strFilter="";
			if(txtFieldCode.Text.Trim()!=""){
					strFilter+=" AND FieldCode LIKE '%"+txtFieldCode.Text.Trim()+"%'";
			}			
			if(txtFieldName.Text.Trim()!=""){
					strFilter+=" AND FieldName LIKE '%"+txtFieldName.Text.Trim()+"%'";
			}			
			if(txtFieldNote.Text.Trim()!=""){
					strFilter+=" AND FieldNote LIKE '%"+txtFieldNote.Text.Trim()+"%'";
			}			
			if(txtFieldType.Text.Trim()!=""){
				strFilter+=" AND FieldType="+txtFieldType.Text.Trim();
			}			
			if(strFilter.Length>0)
				strFilter=strFilter.Substring(4);
			return strFilter;
		}
		//Load All lable text
		private void LoadLanguage(){	
			labelFieldCode.Text=SalaryFieldsStringMapping.FieldCode+":";				
			labelFieldName.Text=SalaryFieldsStringMapping.FieldName+":";				
			labelFieldNote.Text=SalaryFieldsStringMapping.FieldNote+":";				
			labelFieldType.Text=SalaryFieldsStringMapping.FieldType+":";				
			validExpressionFieldType.ErrorMessage=SERP.Constants.APP_REQUIRE_INTERGER_WARNING;			
			btnSearch.Text=SERP.Constants.APP_SEARCH;
			btnAddNew.Text=SERP.Constants.APP_ADDNEW;
			btnDelete.Text=SERP.Constants.APP_DELETE;
			btnHelps.Text=SERP.Constants.APP_HELPS;
            btnDelete.OnClientClick = "return confirm(\"" + Constants.APP_CONFIRM_DELELE + "\")";
			lblTitle.InnerText=SERP.Constants.APP_LIST+" "+SalaryFieldsStringMapping.SalaryFields;
		}
		private void LoadData(bool clearCache){
			//Load data to grid
			string filter=GetFilter();
			controller.LoadAllDSSalaryFields(clearCache);
			controller.Table.DefaultView.RowFilter=filter;
			gridData.DataSource=controller.Table;
			//gridData.PageIndex=GetPageIndex();
			gridData.DataBind();
		}
		private void LoadData()
		{
			LoadData(true);
		}
		private Dictionary<string, string> GetParameter(string stParam)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach (string st in stParam.Split(','))
            {
                string[] arr = st.Split('=');
                dic.Add(arr[0], arr[1]);
            }
            return dic;
        }
		private void DeleteData(){
			for(int indexRow=0;indexRow<gridData.Items.Count;indexRow++){
				RepeaterItem row = gridData.Items[indexRow];
				CheckBox cbkRow=(CheckBox)row.FindControl("cbkSalaryFields");
				if(cbkRow!=null){
					if(cbkRow.Checked){
						LinkButton lkDelete=(LinkButton)row.FindControl("btnXoa");
                        Dictionary<string, string> dic = GetParameter(lkDelete.CommandArgument.ToString().Substring(1));
						controller.DeleteByID(Int32.Parse(dic["SalaryFieldId"]));
					}
				}
			}
		}
		protected void btnSearch_Click(object sender, EventArgs e)
		{
			LoadData(false);
		}
		protected void OnAddNew(object sender, EventArgs e)
		{
			RedirectToPage("SalaryFieldsAddNew");
		}
		protected void OnDelete(object sender, EventArgs e)
		{
			DeleteData();	
			LoadData();
		}
		protected void btnXem_OnCommand(object sender,CommandEventArgs e){
			RedirectToPage("","","SalaryFieldsView",e.CommandArgument.ToString().Trim().TrimStart('&'));
		}
		protected  void btnSua_OnCommand(object sender, CommandEventArgs e)
		{
			RedirectToPage("", "", "SalaryFieldsEdit", e.CommandArgument.ToString().Trim().TrimStart('&'));
		}
		protected  void btnXoa_OnCommand(object sender, CommandEventArgs e)
		{
			RedirectToPage("", "", "SalaryFieldsDelete", e.CommandArgument.ToString().Trim().TrimStart('&'));
		}
	}
}
